package com.example.book.api;

import com.example.book.base.ApiBaseController;
import com.example.book.base.Result;
import com.example.book.cms.entity.CmsCarousel;
import com.example.book.cms.service.CmsCarouselService;
import com.example.book.common.annotation.LoginIgnore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cms")
public class ApiCmsCarouselController extends ApiBaseController {

    private CmsCarouselService cmsCarouselService;

    private ApiCmsCarouselController(CmsCarouselService cmsCarouselService) {
        this.cmsCarouselService = cmsCarouselService;
    }

    /**
     * 首页轮播图
     *
     * @return
     */
    @GetMapping("/carousel")
    @LoginIgnore
    public Result findCarousel() {
        CmsCarousel carousel = new CmsCarousel();
        // 移动轮播图
        carousel.setType("1");
        carousel.setStatus("0");
        List<CmsCarousel> list = cmsCarouselService.findList(carousel);
        List<Map<String, Object>> maps = new ArrayList<>();
        Map<String, Object> map = null;
        for (CmsCarousel cc : list) {
            map = new HashMap<>();
            map.put("pictureUrl", picture_url + cc.getPictureUrl());
            map.put("title", cc.getTitle());
            maps.add(map);
        }
        if (list.size() < 1) {
            return Result.error("no data");
        }
        return Result.success(maps);
    }

}
